package com.lashou.labelutil.web.action;



import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.lashou.labelutil.model.GoodsCategoryList;
import com.lashou.labelutil.model.GoodsProfileBean;
import com.lashou.labeutil.util.TimeUtil;




public class LabelUtilAction extends BaseAction {

	
	public ActionForward initPage(ActionMapping mapping, ActionForm arg1,
			HttpServletRequest request, HttpServletResponse arg3) throws Exception {
		try{
			
			
			
			boolean ok = this.getLabelService().checkLabelStaut();
			System.out.println("检查状况..."+ok);
			
			if(ok)
			{
				GoodsProfileBean goods = this.getLabelService().findLastGoodsProfileBean();
				
				if(null==goods)
				{
					return mapping.findForward("endedit");
				}
				
				List<GoodsCategoryList> categoryList = this.getLabelService().findAllGoodsCategoryList();
				System.out.println(goods.getGoodsId()+"  "+goods.getCategory()+"   "+goods.getTitle());
				request.setAttribute("oldcate", goods.getCategory());
				if(null!=goods.getCategory())
				{
					String [] ss = goods.getCategory().trim().split(";");
					if(ss != null && ss.length >0)
					{
						request.setAttribute("oldcategorys", ss);
					}
				}
				request.setAttribute("categoryList", categoryList);
				request.setAttribute("goods", goods);
				request.setAttribute("count", this.getLabelService().getRemainCount());
				
				return mapping.findForward("success");
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return mapping.findForward("endedit");
	}
	
	public ActionForward editGoodsProfile(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String goodsId = request.getParameter("goodsId");
		if(null == goodsId || "".equals(goodsId))
		{
			return new ActionForward("/label.do?method=initPage");
		}
		
		String [] values = request.getParameterValues("cates");
		String oldcate = request.getParameter("oldcate");
		oldcate = this.getBaseCategory(oldcate);
		if(null != values && values.length>0)
		{
			for(String v : values)
			{
				if(!oldcate.contains(v))
				{
					oldcate += (v+";");
				}
			}
			
		}
		
		System.out.println("Edit......"+goodsId);
		GoodsProfileBean bean = new GoodsProfileBean();
		bean.setGoodsId(Integer.valueOf(goodsId));
		bean.setCategory(oldcate);
		System.out.println(oldcate);
		this.getLabelService().closeEditLabel(bean);
		
		
		return new ActionForward("/label.do?method=initPage");
	}
	
	
	public ActionForward initPageList(ActionMapping mapping, ActionForm arg1,
			HttpServletRequest request, HttpServletResponse arg3) throws Exception {
		try{

			
			String count = request.getParameter("count");
			String categoryType = request.getParameter("categoryType");
			String currpage = request.getParameter("currpage");
			
			
		
			count = (null==count?"100":count);
			categoryType = (null==categoryType?"-1":categoryType);
			currpage = (null==currpage?"1":currpage);
			
			System.out.println("categoryType  === "+categoryType);

			
			List<GoodsCategoryList> categoryList = this.getLabelService().findAllGoodsCategoryList();
			List<GoodsProfileBean> goodsprofile = this.getLabelService().findGoodsProfileByCategoryType(new Integer(categoryType), new Integer(count)*(new Integer(currpage)-1), new Integer(count));
			int goodsprofileTotal = this.getLabelService().findGoodsProfileTotal(new Integer(categoryType));
			
			for(GoodsProfileBean bean:goodsprofile)
			{
				if(null!=bean.getCategory())
				{
					bean.setCategorys(bean.getCategory().trim().split(";"));
				}
			}
			System.out.println("goodsprofile size = "+goodsprofile.size());
			System.out.println("goodsprofileTotal = "+goodsprofileTotal);
			
			request.setAttribute("categoryList", categoryList);
			request.setAttribute("goodsprofile", goodsprofile);
			request.setAttribute("goodsprofileTotal", goodsprofileTotal);
			request.setAttribute("categoryType", categoryType);
			
			request.setAttribute("count", count);
			request.setAttribute("currpage", currpage);
			request.setAttribute("totalPage", goodsprofileTotal/new Integer(count)+(goodsprofileTotal%new Integer(count)==0?0:1));
			return mapping.findForward("editlist");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public ActionForward ajaxEditProfile(ActionMapping mapping, ActionForm arg1,
			HttpServletRequest request, HttpServletResponse arg3) throws Exception {
			try{
				String goodsId = request.getParameter("goodsId");
				if(null == goodsId || "".equals(goodsId))
				{
					return null;
				}
				GoodsProfileBean bean = this.getLabelService().findGoodsProfileBeanById(new Integer(goodsId));
				if(null == bean)
				{
					return null;
				}
				List<GoodsProfileBean> beans = this.getLabelService().findGoodsProfileBeanByTitle(bean.getTitle());
				
				
				System.out.println("beans size ===="+beans.size());
				String data = request.getParameter("data");
				for(GoodsProfileBean b:beans)
				{
					System.out.println("goodsId = "+b.getGoodsId()+"   old cate = "+b.getCategory());
					String oldcate = this.getBaseCategory( b.getCategory());
					String [] datas = data.split(",");
					          
					if(null != datas && datas.length>0)
					{
						for(String v : datas)
						{
							if(!oldcate.contains(v))
							{
								oldcate += (v+";");
							}
						}
						
					}
					b.setCategory(oldcate);
					System.out.println("goodsId = "+b.getGoodsId()+"  cate = "+b.getCategory());
					this.getLabelService().updateGoodsProfileBean(b);
				}
				return null;
			}catch(Exception e)
			{
				e.printStackTrace();
			}
			return null;
		}
	private String getBaseCategory(String category)
	{
		String rs = "";
		try{
			if(null == category || "".equals(category))
			{
				return "";
			}
			
			String [] cates = category.split(";");
			for(String s:cates)
			{
				if(new Integer(s) < 100)
				{
					rs +=(s+";");
				}
			}
		}
		catch(Exception e)
		{
			
		}
		return rs;
	}
	
	public ActionForward initLastGoodsPage(ActionMapping mapping, ActionForm arg1,
			HttpServletRequest request, HttpServletResponse arg3) throws Exception {
	
			String count = request.getParameter("count");
			String currpage = request.getParameter("currpage");
			String date = request.getParameter("up_date");
			if(null == date || "".equals(date))
			{
				date = request.getParameter("down_date");
			}
			if(null == date || "".endsWith(date))
			{
				date = TimeUtil.getDateByForamt(new java.util.Date(), "yyyy-MM-dd 00:00:00");
			}
			
			count = (null==count?"100":count);
			currpage = (null==currpage?"1":currpage);
		
			List<GoodsCategoryList> categoryList = this.getLabelService().findAllGoodsCategoryList();
			List<GoodsProfileBean> goodsprofile = this.getLabelService().findLastProfileByDate(date,new Integer(count)*(new Integer(currpage)-1), new Integer(count));//this.getLabelService().findGoodsProfileByCategoryType(new Integer(categoryType), new Integer(count)*(new Integer(currpage)-1), new Integer(count));
			int goodsprofileTotal = this.getLabelService().findTotalLastProfilebyDate(date);//(new Integer(categoryType));
			
			for(GoodsProfileBean bean:goodsprofile)
			{
				if(null!=bean.getCategory())
				{
					bean.setCategorys(bean.getCategory().trim().split(";"));
				}
			}
			System.out.println("goodsprofile size ======"+goodsprofile.size() +" goodsprofileTotal====="+goodsprofileTotal);
			
			request.setAttribute("categoryList", categoryList);
			request.setAttribute("goodsprofile", goodsprofile);
			request.setAttribute("goodsprofileTotal", goodsprofileTotal);

			request.setAttribute("date", date);
			request.setAttribute("count", count);
			request.setAttribute("currpage", currpage);
			request.setAttribute("totalPage", goodsprofileTotal/new Integer(count)+(goodsprofileTotal%new Integer(count)==0?0:1));
			return mapping.findForward("lastgoodslist");
			}

}
